package com.zyx.crawlerdemo.httpclient.crawler;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Yaxi.Zhang
 * @since 2021/8/11 17:09
 * desc: post提交案例
 */
public class HttpclientRenren {
	public static void main(String[] args) throws ParseException, IOException {
		// 初始化httpclient
		HttpClient httpclient = HttpClients.custom().build();
		// 登陆的地址
		String renRenLoginUrl = "http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2018922138705";
		// 采用post方法
		HttpPost httpost = new HttpPost(renRenLoginUrl);
		// 建立一个NameValuePair数组，用于存储欲传送的参数
		List<NameValuePair> nvps = new ArrayList<>();
		// 输入你的邮箱地址
		nvps.add(new BasicNameValuePair("email", "******"));
		// 输入你的密码
		nvps.add(new BasicNameValuePair("password", "******"));
		HttpResponse response = null;
		try {  
			// 表单参数提交
			httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			response = httpclient.execute(httpost); 
		} catch (Exception e) {  
			e.printStackTrace();  
		} finally {  
			//释放连接
			httpost.abort(); 
		}  
		System.out.println(response.getStatusLine());
		// 注意设置编码
		String entityString = EntityUtils.toString (response.getEntity(),"gbk");
		System.out.println(entityString);
		// 登录完成之后需要请求的内容，这里是我个人好友
		HttpGet httpget = new HttpGet("http://www.renren.com/465530468/profile?v=info_timeline");  
		// 构建一个 responseHandler
		ResponseHandler<String> responseHandler = new BasicResponseHandler();  
		String responseBody = "";  
		try {  
			responseBody = httpclient.execute(httpget, responseHandler);  
		} catch (Exception e) {  
			e.printStackTrace();  
			responseBody = null;  
		} finally {  
			// 释放连接
			httpget.abort();  
		}  
		// 输出请求到的内容
		System.out.println(responseBody);
	}
}
